
#include <stdio.h>
#include <vector>

#include "UDPSocket.h"
#include "five_machine.h"
#include "cQiju.h"

#define SERVER_PORT 2008

using namespace std;

int find_machine(vector<five_machine>& m,socket_addr &addr)
{
	int i = 0;
	for(vector<five_machine>::iterator it = m.begin();it != m.end(); it++){
	  if(it->getaddr() == addr){
	    return i;
	  }
	  i ++;
	}
	return -1;
}



int main(int argc, char* argv[])
{
	five_pkt pkt_in,pkt_out;
	vector<five_machine> machines;
	UDP_socket sock("127.0.0.1",SERVER_PORT);

	int machine_size = 0;
	while (1){
		sock.recv_from((char *)&pkt_in,sizeof(pkt_in));

		int m = find_machine(machines,sock.get_recv_addr());

		if( m == -1){
			five_machine machine(sock.get_recv_addr());
			machines.push_back(machine);
			m = machines.size() - 1;
			machines[m].id = m;
		}
		machines[m].process_pkt(pkt_in,sock,&machines);		
	}

	sock.~UDP_socket();
	UDPSocket::Shutdown();
	return 0;
}
